<?php
class omedlyexport_mdl_ome_delivery extends ome_mdl_delivery{
    public function getList($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
        $deliveryObj = &app::get('ome')->model('delivery');
        $type=$_GET['type'];
        
        if($type=='all'){
        	unset($filter['delivery_id']);    
        }
        $filterSql = $deliveryObj->_filter($filter,$tableAlias,$baseWhere);
     
     
        $deliveryColumns = array_keys($deliveryObj->_columns($filter,$tableAlias,$baseWhere));
        foreach($deliveryColumns as $col){
            if($col == 'delivery'){
                continue;
            }
            $filterSql = str_replace($col,'D.'.$col,str_replace('`sdb_ome_delivery`.','',$filterSql));
        }
        if($filterSql){
            $whereSql = ' WHERE '.$filterSql;
        }
        if($type=='all'){        
        	$whereSql .=" and O.shop_id='9da579eab3662cc0086b89cb65b23198'";
        }
        $sql = 'SELECT O.order_bn,O.shop_id,O.tax_no,D.member_id,D.logi_name,DI.bn,DI.product_name,DI.number,OI.price,
            ROUND((O.cost_freight/OI.nums)*DI.number,3) AS freight,ROUND((O.cost_freight/OI.nums+OI.price)*DI.number,3) as total,
            D.logi_no  FROM 
            sdb_ome_delivery_items AS DI LEFT JOIN 
            sdb_ome_delivery AS D ON D.delivery_id = DI.delivery_id LEFT JOIN 
            sdb_ome_delivery_order AS DO ON DO.delivery_id = D.delivery_id LEFT JOIN 
            sdb_ome_orders AS O ON O.order_id = DO.order_id LEFT JOIN 
            sdb_ome_order_items AS OI ON OI.order_id = O.order_id AND DI.bn=OI.bn '.$whereSql;
 //echo $sql;
        $rows = $this->db->selectLimit($sql,$limit,$offset);
        foreach($rows as $key=>$row){
            $member = array();
            $memberObj = &app::get('ome')->model('members');
            $member = $memberObj->getList('uname',array('member_id'=>$row['member_id']),0,1);
            $rows[$key]['member_id'] = $member[0]['uname'];
        }
        return $rows;
    }

    public function get_schema(){
        $schema = array (
            'columns' => array (
                'order_bn' => array (
                    'type' => 'varchar(32)',
                    'required' => true,
                    'label' => '订单号',
                    'width' => 125,
                    'editable' => false,
                ),
                'shop_id' => array (
                    'type' => 'table:shop@ome',
                    'label' => '来源店铺',
                    'width' => 75,
                    'editable' => false,
                ),
                'tax_no' => array (
                    'type' => 'varchar(50)',
                    'label' => '发票号',
                    'editable' => false,
                ),
                'member_id' => array (
                    'type' => 'varchar(50)',
                    'label' => '会员用户名',
                    'comment' => '订货会员ID',
                    'editable' => false,
                    'width' =>75,
                ),
                'logi_name' => array (
                    'type' => 'varchar(100)',
                    'label' => '物流公司',
                    'comment' => '物流公司名称',
                    'editable' => false,
                    'width' =>75,
                ),
                'bn' => array (
                    'type' => 'varchar(30)',
                    'label' => '商品货号',
                    'width' => 85,
                    'editable' => false,
                ),
                'product_name' => array (
                    'type' => 'varchar(200)',
                    'required' => true,
                    'default' => '',
                    'label' => '商品名称',
                    'width' => 190,
                    'editable' => false,
                ),
                'number' => array (
                    'type' => 'number',
                    'required' => true,
                    'default' => 0,
                    'label' => '购买数量',
                    'editable' => false,
                ),
                'price' => array (
                    'type' => 'money',
                    'default' => '0',
                    'required' => true,
                    'label' => '商品单价',
                    'editable' => false,
                ),
                'freight' => array (
                    'type' => 'money',
                    'default' => '0',
                    'required' => true,
                    'label' => '配送费用',
                    'width' => 70,
                    'editable' => false,
                ),
                'total' => array (
                    'type' => 'money',
                    'default' => '0',
                    'label' => '总价',
                    'width' => 70,
                    'editable' => false,
                ),
            	
            		'logi_no' => array (
            				'type' =>'varchar(20)',
            				'default' => '0',
            				'label' => '快递单号',
            				'width' => 70,
            				'editable' => false,
            		),
            ),
            'idColumn' => 'bn',
            'in_list' => array (
                0 => 'order_bn',
                1 => 'shop_id',
                2 => 'tax_no',
                3 => 'member_id',
                4 => 'logi_name',
                5 => 'bn',
                6 => 'product_name',
                7 => 'number',
                8 => 'price',
                9 => 'freight',
                10 => 'total_amount',
            	
            		11 => 'logi_no',
            ),
            'default_in_list' => array (
                0 => 'order_bn',
                1 => 'shop_id',
                2 => 'tax_no',
                3 => 'member_id',
                4 => 'logi_name',
                5 => 'bn',
                6 => 'product_name',
                7 => 'number',
                8 => 'price',
                9 => 'freight',
                10 => 'total_amount',
            	
            		11 => 'logi_no',
            ),
        );
        return $schema;
    }
}